using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using IWS.Lib.SingleSignOn.ServiceResponses;
using System.Web.Security;

namespace AuthWebService {
	[WebService(Namespace = "http://www.integratedwebsystems.com/Services/", Description = "This is the role provider service.")]
	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
	public class RoleService : System.Web.Services.WebService {

		public RoleService() {

			//Uncomment the following line if using designed components 
			//InitializeComponent(); 
		}
		private bool ValidateSession(string sKey) {
			return IWS.Lib.SingleSignOn.Session.ValidateSession(sKey);
		}

		private void LogException(Exception ex) {

		}

		/// <summary>
		/// Authenticates a session with a username and password and returns the associated session key.
		/// </summary>
		/// <param name="username">Username</param>
		/// <param name="password">Password</param>
		/// <returns>Response contiaining new session key</returns>
		[WebMethod(Description = "Authenticates a session with a username and password and returns the associated session key.")]
		public ServiceResponse AuthenticateSession(string username, string password) {
			ServiceResponse resp = new ServiceResponse();
			try {
				if (Membership.Provider.ValidateUser(username, password)) {
					resp.Result = ServiceResponseBase.eResult.Success;
					IWS.Lib.SingleSignOn.Session session = new IWS.Lib.SingleSignOn.Session();
					session.Initialize();
					MembershipUser user = Membership.GetUser(username);
					session.UserID = (Guid)user.ProviderUserKey;
					session.Save();
					resp.Data = session.Key;
				}
				else {
					resp.Result = ServiceResponseBase.eResult.InvalidLogin;
				}
			}
			catch (Exception ex) {
				resp.Message = ex.Message;
				resp.Result = ServiceResponseBase.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse AddUsersToRoles(string sSessionKey, string[] usernames, string[] roleNames) {
			ServiceResponse resp = new ServiceResponse();
			try {
				if (ValidateSession(sSessionKey)) {
					Roles.Provider.AddUsersToRoles(usernames, roleNames);
					resp.Result = ServiceResponseBase.eResult.Success;
				}
				else {
					resp.Result = ServiceResponseBase.eResult.InvalidSession;
				}
			}
			catch (Exception ex) {
				resp.Message = ex.Message;
				resp.Result = ServiceResponseBase.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse GetApplicationName(string sSessionKey) {
			ServiceResponse resp = new ServiceResponse();
			try {
				if (ValidateSession(sSessionKey)) {
					resp.Data = Roles.Provider.ApplicationName;
					resp.Result = ServiceResponseBase.eResult.Success;
				}
				else {
					resp.Result = ServiceResponseBase.eResult.InvalidSession;
				}
			}
			catch (Exception ex) {
				resp.Message = ex.Message;
				resp.Result = ServiceResponseBase.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}
		[WebMethod]
		public ServiceResponse SetApplicationName(string sSessionKey, string name) {
			ServiceResponse resp = new ServiceResponse();
			try {
				if (ValidateSession(sSessionKey)) {
					Roles.Provider.ApplicationName = name;
					resp.Result = ServiceResponseBase.eResult.Success;
				}
				else {
					resp.Result = ServiceResponseBase.eResult.InvalidSession;
				}
			}
			catch (Exception ex) {
				resp.Message = ex.Message;
				resp.Result = ServiceResponseBase.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse CreateRole(string sSessionKey, string roleName) {
			ServiceResponse resp = new ServiceResponse();
			try {
				if (ValidateSession(sSessionKey)) {
					Roles.Provider.CreateRole(roleName);
					resp.Result = ServiceResponseBase.eResult.Success;
				}
				else {
					resp.Result = ServiceResponseBase.eResult.InvalidSession;
				}
			}
			catch (Exception ex) {
				resp.Message = ex.Message;
				resp.Result = ServiceResponseBase.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse DeleteRole(string sSessionKey, string roleName, bool throwOnPopulatedRole) {
			ServiceResponse resp = new ServiceResponse();
			try {
				if (ValidateSession(sSessionKey)) {
					resp.Data = Roles.Provider.DeleteRole(roleName, throwOnPopulatedRole);
					resp.Result = ServiceResponseBase.eResult.Success;
				}
				else {
					resp.Result = ServiceResponseBase.eResult.InvalidSession;
				}
			}
			catch (Exception ex) {
				resp.Message = ex.Message;
				resp.Result = ServiceResponseBase.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ArrayServiceResponse FindUsersInRole(string sSessionKey, string roleName, string usernameToMatch) {
			ArrayServiceResponse resp = new ArrayServiceResponse();
			try {
				if (ValidateSession(sSessionKey)) {
					resp.Data = Roles.Provider.FindUsersInRole(roleName, usernameToMatch);
					resp.Result = ServiceResponseBase.eResult.Success;
				}
				else {
					resp.Result = ServiceResponseBase.eResult.InvalidSession;
				}
			}
			catch (Exception ex) {
				resp.Message = ex.Message;
				resp.Result = ServiceResponseBase.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ArrayServiceResponse GetAllRoles(string sSessionKey) {
			ArrayServiceResponse resp = new ArrayServiceResponse();
			try {
				if (ValidateSession(sSessionKey)) {
					resp.Data = Roles.Provider.GetAllRoles();
					resp.Result = ServiceResponseBase.eResult.Success;
				}
				else {
					resp.Result = ServiceResponseBase.eResult.InvalidSession;
				}
			}
			catch (Exception ex) {
				resp.Message = ex.Message;
				resp.Result = ServiceResponseBase.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ArrayServiceResponse GetRolesForUser(string sSessionKey, string username) {
			ArrayServiceResponse resp = new ArrayServiceResponse();
			try {
				if (ValidateSession(sSessionKey)) {
					resp.Data = Roles.Provider.GetRolesForUser(username);
					resp.Result = ServiceResponseBase.eResult.Success;
				}
				else {
					resp.Result = ServiceResponseBase.eResult.InvalidSession;
				}
			}
			catch (Exception ex) {
				resp.Message = ex.Message;
				resp.Result = ServiceResponseBase.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ArrayServiceResponse GetUsersInRole(string sSessionKey, string roleName) {
			ArrayServiceResponse resp = new ArrayServiceResponse();
			try {
				if (ValidateSession(sSessionKey)) {
					resp.Data = Roles.Provider.GetUsersInRole(roleName);
					resp.Result = ServiceResponseBase.eResult.Success;
				}
				else {
					resp.Result = ServiceResponseBase.eResult.InvalidSession;
				}
			}
			catch (Exception ex) {
				resp.Message = ex.Message;
				resp.Result = ServiceResponseBase.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse IsUserInRole(string sSessionKey, string username, string roleName) {
			ServiceResponse resp = new ServiceResponse();
			try {
				if (ValidateSession(sSessionKey)) {
					resp.Data = Roles.Provider.IsUserInRole(username, roleName);
					resp.Result = ServiceResponseBase.eResult.Success;
				}
				else {
					resp.Result = ServiceResponseBase.eResult.InvalidSession;
				}
			}
			catch (Exception ex) {
				resp.Message = ex.Message;
				resp.Result = ServiceResponseBase.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse RemoveUsersFromRoles(string sSessionKey, string[] usernames, string[] roleNames) {
			ServiceResponse resp = new ServiceResponse();
			try {
				if (ValidateSession(sSessionKey)) {
					Roles.Provider.RemoveUsersFromRoles(usernames, roleNames);
					resp.Result = ServiceResponseBase.eResult.Success;
				}
				else {
					resp.Result = ServiceResponseBase.eResult.InvalidSession;
				}
			}
			catch (Exception ex) {
				resp.Message = ex.Message;
				resp.Result = ServiceResponseBase.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse RoleExists(string sSessionKey, string roleName) {
			ServiceResponse resp = new ServiceResponse();
			try {
				if (ValidateSession(sSessionKey)) {
					Roles.Provider.RoleExists(roleName);
					resp.Result = ServiceResponseBase.eResult.Success;
				}
				else {
					resp.Result = ServiceResponseBase.eResult.InvalidSession;
				}
			}
			catch (Exception ex) {
				resp.Message = ex.Message;
				resp.Result = ServiceResponseBase.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}
	}
}